﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class DeliveryPlanApiController : ApiController
    {
        public DeliveryPlanRepository Get(int? pageNumber, string orderBy, string orderDirection, string deliveryPlanCode,string fullName, string datefrom, string dateto)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(deliveryPlanCode))
                    whereClause = String.Format(whereClause + " AND deliveryPlanCode.Contains(\"{0}\")", deliveryPlanCode);
                if (!String.IsNullOrWhiteSpace(fullName))
                    whereClause = String.Format(whereClause + " AND fullName.Contains(\"{0}\")", fullName);

                return new DeliveryPlanRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public DeliveryPlan Get(int? id)
        {
            try
            {
                var item = new DeliveryPlan();
                var deliveryPlanRepository = new DeliveryPlanRepository();
                var orderLineRepository = new OrderLineRepository();
                var orderRepository = new OrderRepository();
                List<int> orderLineIds = new List<int>();
                List<int> planQtys = new List<int>();
                item.DeliveryPlanCode = GenerateCode.GetCode(deliveryPlanRepository.GetNextIndex(), CodeType.DeliveryPlan);
                if (!(!id.HasValue || id == 0))
                {
                    item = deliveryPlanRepository.GetByID(id.Value);
                    foreach (var planLine in item.DeliveryPlanLines)
                    {
                        orderLineIds.Add(planLine.OrderLineID);
                        planQtys.Add(planLine.PlanQty);
                    }
                }                

                List<Order> orders = orderRepository.GetListByStatus("I");                
                item.Orders=new List<Order>();
                List<OrderLine> orderLines;
                item.OrderLines = new List<OrderLine>();
                foreach (var order in orders)
                {
                    orderLines = orderLineRepository.GetList(order, orderLineIds, planQtys);
                    ((List<OrderLine>)item.OrderLines).AddRange(orderLines);
                    if (orderLines.Count > 0) item.Orders.Add(order);
                }
                return item;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var repository = new DeliveryPlanRepository();
                    repository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public DeliveryPlanRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string deliveryPlanCode, string fullName, string datefrom, string dateto)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var repository = new DeliveryPlanRepository();
                repository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection,deliveryPlanCode,fullName,datefrom,dateto);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public DeliveryPlan Put(DeliveryPlan deliveryPlan)
        {
            try
            {
                var repository = new DeliveryPlanRepository();
                int ID = repository.Update(deliveryPlan);
                return Get(ID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        public void Head(string ids) //Điều xe
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');
                var deliveryPlanrepository = new DeliveryPlanRepository();
                deliveryPlanrepository.UpdateStatus(idArr, "I");

                var repository = new CommandRepository();
                repository.CreateCommandFromDeliveryPlan(idArr);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }
    }
}
